<?php
//=======================================
// !! GYSN RELEASE - DO NOT REMOVE  !!
// !! DeZend by: syairx             !!
// !! Debug by: syairx & str/GYSN   !!
// !! Nullify by: str/GYSN & syairx !!
// !! GYSN RELEASE - DO NOT REMOVE  !!
//=======================================

error_reporting( E_ALL & ~E_NOTICE );

if ( PHP_VERSION < "4.1.0" )
{
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
	$_REQUEST = array_merge( $_GET, $_POST, $_COOKIE );
}

$_reserved = array( "_reserved", "GLOBALS", "_GET", "_POST", "_COOKIE", "_SERVER", "_ENV", "_REQUEST", "_FILES", "argv" );

if ( is_array( $GLOBALS ) )
{
	reset( $GLOBALS );
	while ( list( $_key, $_val ) = each( $GLOBALS ) )
	{
		if ( !in_array( $_key, $_reserved ) && ( $_key != "_key" && $_key != "_val" ) )
		{
		unset( $GLOBALS[$_key] );
		}
	}
}

$_DB = array( );
$_SWIFT = array( );
define( "INSWIFT", true );
define( "INSWIFTSETUP", true );

$_SWIFT["setup"] = true;

require_once( "./includes/functions_xml.php" );
$xml = new XML( );

function displayHeader( )
{
	global $_SWIFT;

	require_once( dirname( __FILE__ )."/header.php" );
}

function displayFooter( )
{
	global $_SWIFT;

	require_once( dirname( __FILE__ )."/footer.php" );
}

function displayStatus( $statustext, $success = true, $reason = "", $nofail = false )
{
	global $_SWIFT;
	if ( INTERFACETYPE == "CLI" )
	{
		if ( !$success )
		{
			if ( $nofail != true )
			{
				$_SWIFT["setupfailed"] = true;
			}
			echo "[FAILED]: ".$statustext." (".$reason.")\n";
		}
		else
		{
			echo "[SUCCESS]: ".$statustext.iif( substr( $statustext, strlen( $statustext ) - 1 ) == ":", " OK" )."\n";
		}

		return;
	}

	if ( !$success )
	{
		if ( $nofail != true )
		{
			$_SWIFT["setupfailed"] = true;
		}

		echo '<table width="100%"  border="0" cellspacing="0" cellpadding="4"><tr><td width="74%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'.$statustext.' </font></td><td width="26%" align="right"><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>FAILED</strong></font></td></tr>';
		if ( $reason != "" )
		{
			echo '<tr><td width="100%"><span class="gtext"><font color="blue">Reason: '.$reason.'</font></span></td></tr>';
		}

		echo '</table>';
	}
	else
	{
		echo '<table width="100%"  border="0" cellspacing="0" cellpadding="4"><tr><td width="74%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'.$statustext.'</font></td><td width="26%" align="right"><font color="#438CCA" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>OK</strong></font></td></tr></table>';
	}
	flush( );
}

function displayStatusList( $list )
{
	for ( $ii = 0; $ii < count( $list ); $ii++ )
	{
		displayStatus( $list[$ii]["statustext"], $list[$ii]["success"], $list[$ii]["reason"] );
	}

}

function displayValue( $message, $value, $reason = "" )
{
	echo '<table width="100%"  border="0" cellspacing="0" cellpadding="4"><tr><td width="74%" align="left"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif">'.$message.'</font></td><td width="26%" align="right"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>'.$value.'</b></font></td></tr>';

	if ( $reason != "" )
	{
		echo '<tr><td width="100%"><span class="gtext"><font color="green">'.$reason.'</font></span></td></tr>';
	}
	echo '</table>';
}

function displayMessage( $message )
{
	if ( INTERFACETYPE == "CLI" )
	{
		echo $message."\n";
	}
	else
	{
		echo '<table width="100%"  border="0" cellspacing="0" cellpadding="4"><tr><td width="100%"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>'.$message.'</b></font></td></tr></table>';
	}
}

function displayError( $message )
{
	if ( INTERFACETYPE == "CLI" )
	{
		echo "[ERROR]: ".$message;
	}
	else
	{
		echo '<table width="100%"  border="0" cellspacing="0" cellpadding="4"><tr><td width="100%"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>Error:</b> <font color="darkred">'.$message.'</font></font></td></tr></table>';
	}
}

function displayField( $fieldtitle, $fieldname, $defaultvalue = "", $type = "text", $isdisabled = false )
{
	echo '<table width="100%"  border="0" cellspacing="0" cellpadding="3"><tr><td width="200"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>&nbsp;'.$fieldtitle.':</b></font></td><td align="left"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif">';
	if ( $isdisabled == false )
	{
		echo '<input type="'.$type.'" name="'.$fieldname.'" value="'.$defaultvalue.'" class="inputtext">';
	}
	else
	{
		echo $defaultvalue;
	}
	echo '</font></td></tr></table>';
}

function displayFieldCustom( $fieldtitle, $fielddata )
{
	echo '<table width="100%"  border="0" cellspacing="0" cellpadding="3"><tr><td width="200"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>&nbsp;'.$fieldtitle.':</b></font></td><td align="left"><font color="#333333" size="2" face="Verdana, Arial, Helvetica, sans-serif">';
	echo $fielddata;
	echo '</font></td></tr></table>';
}

function getProductColor( )
{
	if ( SWIFTPRODUCT == "eSupport" )
	{
		return "#193366";
	}
	else if ( SWIFTPRODUCT == "LiveResponse" )
	{
		return "#01758C";
	}
	else if ( SWIFTPRODUCT == "HelpBase" )
	{
		return "#5A6680";
	}
	else
	{
	return "#E55838";
	}
}

function execAllQueries( $silent = false )
{
	global $_SWIFT;

	execQuery( "drop", "Dropping table %s:", $silent );
	execQuery( "create", "Creating table %s:", $silent );
	execQuery( "alter", "Altering table %s:", $silent );
	execQuery( "delete", "Deleting data from %s:", $silent );
	execQuery( "replace", "Replacing data in %s:", $silent );
	execQuery( "update", "Updating data in %s:", $silent );
	execQuery( "insert", "Inserting data into %s:", $silent );
	unset( $_SWIFT["query"] );
}

function execQuery( $arraykey, $displaystr, $silent = false )
{
	global $_SWIFT, $dbCore;

	if ( is_array( $_SWIFT["query"][$arraykey] ) )
	{
		foreach ( $_SWIFT["query"][$arraykey] as $key => $val )
		{
			$error = "";
			if ( is_array( $_SWIFT["query"][$arraykey][$key] ) )
			{
				$total = count($_SWIFT["query"][$arraykey][$key]);
				for ($ii=0; $ii<$total; $ii++)
				{
					$error = "";
					if ($_SWIFT["setupfailed"] != true)
					{
						$result = $dbCore->query($_SWIFT["query"][$arraykey][$key][$ii], 2);
						$error = $dbCore->fetchLastError();
					}
					else
					{
						$result = false;
					}
				}

				if ( !$silent )
				{
					displayStatus( sprintf( $displaystr, $key ).( " (# Of Queries: ".$total.")" ), $result, $error );
				}
			}
			else
			{
				if ( $_SWIFT["setupfailed"] != true )
				{
					$result = $dbCore->query( $val, 2 );
					$error = $dbCore->fetchLastError( );
				}
				else
				{
				$result = false;
				}

				if ( !$silent )
				{
					displayStatus( sprintf( $displaystr, $key ), $result, $error );
				}
			}
		}
	}
	unset( $_SWIFT["query"][$arraykey] );
}

function setup_cleardb( )
{
	global $dbCore;

	$resultlist = array( );
	$cnt = 0;

	$dbCore->query( "SHOW TABLE STATUS;" );
	while ( $dbCore->nextRecord( ) )
	{
		$result = $dbCore->query( sprintf( "DROP TABLE %s;", $dbCore->Record["Name"] ), 2 );
		$resultlist[$cnt]["statustext"] = sprintf( "Dropping Table %s", $dbCore->Record["Name"] );
		$resultlist[$cnt]["success"] = $result;
		$resultlist[$cnt]["reason"] = $dbCore->fetchLastError( );
		$cnt++;
	}

	return $resultlist;
}

function getSetupModules( )
{
	$_modules = array();

	if ($handle = opendir('./modules/')) {
		while (false !== ($file = readdir($handle))) {
			$file = cleanVar($file);

			if ($file != "." && $file != ".." && $file != "CVS" && is_dir("./modules/".$file) && file_exists("./modules/".strtolower($file)."/setup.php") && $file != "core") {
				$_modules[] = strtolower($file);
			}
		}
		closedir($handle);
	}
	array_unshift($_modules, "core");

	return $_modules;
}

function upgrade_updateVersion( $version )
{
	global $_SWIFT, $dbCore;

	$dbCore->query( "UPDATE `".TABLE_PREFIX."settings` SET data = '".addslashes( $version )."' WHERE `section` = 'core' AND `vkey` = 'version';", 3 );
}

function getVersions( )
{
	$versiontable = array();
	if ($handle = opendir('./setup/upgrade/')) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != ".." && $file != "CVS" && $file != "/" && is_dir("./setup/upgrade/".$file)) {
				$versiontable[] = $file;
			}
		}
		closedir($handle);
	}

	return $versiontable;
}

function getNextVersion( $currentversion )
{
	$versiontable = getVersions( );

	array_multisort( $versiontable, SORT_ASC, SORT_REGULAR );

	for ( $ii = 0; $ii < count( $versiontable ); $ii++ )
	{

		if ( version_compare( $currentversion, $versiontable[$ii] ) == -1 )
		{

			return $versiontable[$ii];
		}
	}

	return false;
}

function getUpgradeVersionTable( $currentversion )
{
	$versiontable = getVersions( );

	array_multisort( $versiontable, SORT_ASC, SORT_REGULAR );

	$versions = array( );

	for ($ii=0; $ii<count($versiontable); $ii++)
	{

		if (version_compare($currentversion, $versiontable[$ii]) == -1)
		{

			$versions[] = $versiontable[$ii];
		}
	}

	return $versions;
}

function rebuildAllCoreCaches( )
{
	require_once( "./includes/functions_settings.php" );
	require_once( "./includes/functions_template.php" );
	require_once( "./includes/functions_language.php" );
	require_once( "./includes/functions_users.php" );
	require_once( "./includes/functions_staff.php" );

	rebuildSettingsCache( );

	rebuildPhraseCache( 1 );
	rebuildLanguageCache( );

	rebuildTemplateGroupCache( );

	rebuildUserGroupPermissions( );
	rebuildStaffGroupPermissions( );

	rebuildGroupSettingsCache( );
	rebuildUserGroupSettingsCache( );

	rebuildTemplateGroupAssignCache( );
}

function processCLIVariables( )
{
	global $argv;

	$_result = array( );
	if ( !is_array( $argv ) )
	{
		return $_result;
	}

	foreach ( $argv as $key => $val )
	{
		if (substr($val,0,1) == "-" && stristr($val, "="))
		{

			$name = substr( $val, 1, ( strpos( $val, "=" ) - 1 ) );
			$value = substr( stristr( $val, "=" ), 1 );
			$_result[$name] = $value;
		}
	}

	return $_result;
}
?>